package com.xyy.saas.payment.trace;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 订单报警消息详情信息
 *
 * @author yejunjian
 * @createDate 2021/10/26 16:31
 */
@Component
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DingTalkMessage {
    private static final Logger LOGGER = LoggerFactory.getLogger(DingTalkMessage.class);
    private String system;
    private String type;
    private String method;
    private String msg;
    private String param;

    @Override
    public String toString() {
        return "告警程序：" + (StringUtils.isEmpty(system) ? "xyy-saas-payment" : system) + "\n" +
                "故障类型：" + type + "\n" +
                "当前时间：" + getNowTime() + "\n" +
                "方法名称：" + method + "\n" +
                "服务器：" + getAddress() + "\n" +
                "相关参数：" + param + "\n" +
                "日志摘要：" + getMsg() + "\n";
    }

    /**
     * 获取服务器地址
     *
     * @return
     */
    public static String getAddress() {
        try {
            InetAddress address = InetAddress.getLocalHost();
            String hostAddress = address.getHostAddress();
            return hostAddress;
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return null;
    }

    public static String getNowTime() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        //获取当前时间
        return LocalDateTime.now().format(dateTimeFormatter);
    }

    public String getMsg() {
        return StringUtils.left(msg, 700);
    }
}

